package com.googlecode.jcaffe.control.modules.mainwindow;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import net.miginfocom.swing.MigLayout;
import com.l2fprod.common.swing.StatusBar;

@SuppressWarnings("serial")
final class MainWindowView extends JFrame {
	private ImageIcon icon;
	private JMenuBar menuBar;
	private JMenu application, helpMenu;
	private JMenuItem connectItem, disconnectItem, settingsItem, exitItem, aboutItem;
	private StatusBar statusBar;

	public MainWindowView() {
		initializeFrame();
		initializeMenuBar();
		initilizeStatusBar();
	}

	private void initializeFrame() {
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setExtendedState(MAXIMIZED_BOTH);
		setTitle("jCaffe Control");
		setLayout(new MigLayout("wrap 1, fill"));

		//Added icon to Main frame.
		icon = new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/icon.png"));
		setIconImage(icon.getImage());
	}

	private void initializeMenuBar() {
		//Create menu bar.
		menuBar = new JMenuBar();

		//Create menu application in menu bar.
		application = new JMenu("Application");
		application.setMnemonic(KeyEvent.VK_A);

		//Added menu to menu bar.
		menuBar.add(application);

		//Create menu help in menu bar.
		helpMenu = new JMenu("Help");
		helpMenu.setMnemonic(KeyEvent.VK_H);

		//Added menu to menu bar.
		menuBar.add(helpMenu);

		//Create menu item connect, set image icon, create shortcuts.
		connectItem = new JMenuItem("Connect", new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/connect.png")));
		connectItem.setMnemonic(KeyEvent.VK_C);
		connectItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK));

		//Added menu item connect to menu application.
		application.add(connectItem);

		//Create menu item disconnect, set image icon, create shortcuts and enabled status.
		disconnectItem = new JMenuItem("Disconnect", new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/disconnect.png")));
		disconnectItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
		disconnectItem.setEnabled(false);

		//Added menu item disconnect to menu application.
		application.add(disconnectItem);

		//Separated connect and disconnect item in one group.
		application.addSeparator();

		//Create menu item settings, set image icon, create shortcuts and action command.
		settingsItem = new JMenuItem("Settings", new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/settings.png")));
		settingsItem.setMnemonic(KeyEvent.VK_S);
		settingsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
		settingsItem.setActionCommand("settings");

		//Added menu item settings to menu application.
		application.add(settingsItem);


		application.addSeparator();

		//Create menu item exit,set image icon, create shortcuts and action command.
		exitItem = new JMenuItem("Exit", new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/exit.png")));
		exitItem.setMnemonic(KeyEvent.VK_X);
		exitItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
		exitItem.setActionCommand("exit");

		//Added menu item exit to menu application.
		application.add(exitItem);

		//Create menu item about, set image icon, create shortcuts.
		aboutItem = new JMenuItem("About", new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/about.png")));
		aboutItem.setMnemonic(KeyEvent.VK_A);

		//Added menu item about to menu help.
		helpMenu.add(aboutItem);

		setJMenuBar(menuBar);
	}

	private void initilizeStatusBar() {
		//Create status bar.
		statusBar = new StatusBar();
		//Create zones in status bar.
		String[] zones = {"connection", "details"};
		JComponent[] components = {new JLabel(), new JLabel()};

		String[] dimensions = {"10%", "90%"};
		statusBar.setZones(zones, components, dimensions);

		add(statusBar, "dock south");

		JLabel test = (JLabel) statusBar.getZone("connection");
		test.setText("Disconnected");
	}

	// Set event listener to menu item settings and exit.
	void setEventListener(MainWindowController controller) {
		settingsItem.addActionListener(controller);
		exitItem.addActionListener(controller);
	}
}
